package day17.feifei.wangyunfei;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class Book implements Serializable {
    private String name;
    private String author;
    private String press;
    private Integer version;
    private Double price;

    public static void main(String[] args) throws Exception {
        List<Book> list = new ArrayList<>();
        list.add(new Book("1", "1", "1", 1, 1.00));
        list.add(new Book("2", "2", "2", 2, 2.00));
        list.add(new Book("3", "3", "3", 3, 3.00));
        persist(list);
        restore();
    }
public static void restore() throws Exception {
    File file=new File("D:\\file\\io\\books.dat");
    FileInputStream fos=new FileInputStream(file);
    ObjectInputStream ois = new ObjectInputStream(fos);
    List<Book> books = (List<Book>)ois.readObject();
    for (Book book : books) {
        System.out.println(book);
    }
}
    public static void persist(List<Book> list) throws IOException {
        File file=new File("D:\\file\\io\\books.dat");
        FileOutputStream fos=new FileOutputStream(file);
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(list);
        oos.flush();
        oos.close();
        fos.close();
    }

    public Book() {
    }

    public Book(String name, String author, String press, Integer version, Double price) {
        this.name = name;
        this.author = author;
        this.press = press;
        this.version = version;
        this.price = price;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * 设置
     *
     * @param author
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * 获取
     *
     * @return press
     */
    public String getPress() {
        return press;
    }

    /**
     * 设置
     *
     * @param press
     */
    public void setPress(String press) {
        this.press = press;
    }

    /**
     * 获取
     *
     * @return version
     */
    public Integer getVersion() {
        return version;
    }

    /**
     * 设置
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * 获取
     *
     * @return price
     */
    public Double getPrice() {
        return price;
    }

    /**
     * 设置
     *
     * @param price
     */
    public void setPrice(Double price) {
        this.price = price;
    }

    public String toString() {
        return "Book{name = " + name + ", author = " + author + ", press = " + press + ", version = " + version + ", price = " + price + "}";
    }
}
